package com.title4;

import java.io.*;
import java.util.HashSet;
import java.util.Scanner;

/**
 * @Author milian
 * @Date 2021/8/4 0004 20:08
 */
public class title4 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请选择操作：1.登录  2.注册  3.退出");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    login();
                    break;
                case "2":
                    register();
                    break;
                case "3":
                    System.out.println("感谢使用");
                    System.exit(0);
            }
        }
    }

    private static void register()throws IOException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);
        HashSet<user> users = getUsers();
        System.out.println("请输入用户名：");
        String username = sc.next();
        String password = null;
        while (true) {
           if (!users.isEmpty()) {
                for (user user : users) {
                    if (!user.getUsername().equals(username)){
                        System.out.println("请输入密码：");
                        System.out.println("1");
                        password = sc.next();
                    }else {
                        System.out.println("用户名已存在！");
                    }
                }
            }else {
               System.out.println("请输入密码：");
               password = sc.next();
           }
            break;
        }
        if (password!=null){
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("H:\\homework\\day13\\user.txt",true));
            oos.writeObject(new user(username,password));
            oos.close();
            System.out.println("注册成功");
        }


    }

    private static void login() throws IOException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);
        HashSet<user> users = getUsers();
        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        if (!users.isEmpty()) {
            for (user user : users) {
                if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                    System.out.println("登录成功");
                } else {
                    System.out.println("登录失败");
                }
            }
        }else {
            System.out.println("登录失败");
        }
    }

    private static HashSet<user> getUsers() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = null;
        HashSet<user> users = new HashSet<>();
        if (new File("H:\\homework\\day13\\user.txt").length()!=0) {
            ois = new ObjectInputStream(new FileInputStream("H:\\homework\\day13\\user.txt"));
            Object len;
            try {
                while ((len = ois.readObject())!= null) {
                    user u = (user) len;
                    users.add(u);
                }
            } catch (EOFException e){

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                ois.close();
            }
        }
        return users;
    }
}